package com.yzj.android.servlet;

import com.google.gson.Gson;
import com.yzj.Bean.User;
import com.yzj.android.service.CakeService;
import com.yzj.android.service.UserService;
import com.yzj.android.service.impl.CakeServiceImpl;
import com.yzj.android.service.impl.UserServiceImpl;
import com.yzj.android.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class UserServlet  extends  BaseServlet{

    /**
     * 处理登录的功能
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("尝试登录。。");
        UserService userService = new UserServiceImpl();
        //  1、获取请求的参数
        User temp = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        // 调用 userService.login()登录处理业务
        Gson gson = new Gson();
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        User loginUser = userService.userLogin(temp.getUsername(), temp.getPassword());
        // 如果等于null,说明登录 失败!
        if (loginUser == null) {
            //  返回错误信息
            resp.getWriter().write("no");
        } else {
            resp.getWriter().write("yes");
        }


    }



    protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService userService = new UserServiceImpl();
        //  1、获取请求的参数
        User temp = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        // 调用 userService.login()登录处理业务
        Gson gson = new Gson();
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        userService.addUser(temp);
        resp.getWriter().write("yes");

        }


    }




